<template>
    <div class="detail">
        <!-- 左侧商品图片 -->
        <div class="dl">
            <img src="../../assets/images/imgsearch1.jpg" alt="">
        </div>
        <div class="rl">
            <h3>商品名称：{{ product.productName }}</h3>
            <p style="margin-top: 10px; font-size: 14px;">商品价格：${{ product.productPrice }}</p>
            <div class="to">
                <div class="tocart" @click="add">
                    加入购物车
                </div>
                <div class="tocart" @click="goCart">
                    立即购买
                </div>
            </div>
        </div>


    </div>
</template>

<script>
import axios from 'axios'
export default {

    data() {

        return {
            pId: '',
            product: {

            }
        }
    },
    mounted() {
        this.pId = localStorage.getItem('pId')
        this.getProduct()
    },
    methods: {
        getProduct() {
            axios({
                headers: {
                    'token': localStorage.getItem('token')
                },
                method: "get",
                url: "http://localhost:8085/product/getProduct",
                params: {
                    productId: parseInt(this.pId),

                }
            }).then(res => {
                //1. 获取分类的数据
                this.product = res.data.data.data
            })

        },
        add() {


            //添加购物车
            axios({
                headers: {
                    'token': localStorage.getItem('token')
                },
                method: "get",
                url: "http://localhost:8085/cart/addCart",
                params: {

                    productId: parseInt(this.pId),

                }
            }).then(res => {
                this.$router.push('/shopcart')
            })

            // axios.post("http://localhost:8081/cart/addCart",{
            // 	productId:this.product.productId,
            // 	userId:this.currentUser.userId,
            // 	cartNum:this.num
            // }).then(function(res){
            // 	if(res.data.code==200){
            // 		alert("加入购物车成功")
            // 	}
            // })

        },
        goCart() {
            if (this.currentUser.username) {
                this.$router.push('/shopcart')
            } else {
                this.$router.push('/login')
            }
        }
    }
}
</script>

<style scoped>
.dl {
    float: left;
    margin-left: 20px;
}

.to {
    margin-top: 230px;
}

.tocart {
    float: left;
    font-size: 16px;
    line-height: 40px;
    text-align: center;
    margin-left: 10px;
    width: 100px;
    height: 40px;
    background-color: red;
}

.dl img {
    width: 300px;
    height: 300px;
}

.rl {
    float: left;
    margin-left: 20px;
}
</style>